<template>
  <div class="app-container">
    <el-form ref="form"
             :model="form"
             :rules="rules"
             :label-position="labelPosition"
             class="register-form">
      <el-row :gutter="40">
        <el-col :span="8">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item prop="firmName" label="事务所名称">
                <el-input v-model="form.firmName" type="text" auto-complete="off" placeholder="请输入事务所名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="officeNumber" label="事务所编号">
                <el-input v-model="form.officeNumber" type="text" auto-complete="off" placeholder="请输入事务所编号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="typeOfFirm" label="事务所类型">
                <el-input v-model="form.typeOfFirm" type="text" auto-complete="off" placeholder="请输入事务所类型" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="haveSecuritiesQualification" label="是否有证券资格">
                <el-select v-model="form.haveSecuritiesQualification" placeholder="请选择">
                  <el-option label="是" value="是" />
                  <el-option label="否" value="否" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="haveHShareQualification" label="是否有 H 股资格">
                <el-select v-model="form.haveHShareQualification" placeholder="请选择">
                  <el-option label="是" value="是" />
                  <el-option label="否" value="否" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="numberOfCpas" label="注册会计师数量">
                <el-input v-model="form.numberOfCpas" type="text" auto-complete="off" placeholder="请选择" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="numberOfBranchRegisteredEngineers" label="其中分所注师数量">
                <el-input v-model="form.numberOfBranchRegisteredEngineers" type="text" auto-complete="off" placeholder="请输入其中分所注师数量" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="headOfficeOrBranchOffice" label="总所/分所">
                <el-select v-model="form.headOfficeOrBranchOffice" placeholder="请选择">
                  <el-option label="总所" value="总所" />
                  <el-option label="分所" value="分所" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="placeOfBusiness" label="经营场所">
                <el-input v-model="form.placeOfBusiness" type="text" auto-complete="off" placeholder="请输入经营场所" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="officeSignNo" label="事务所字号">
                <el-input v-model="form.officeSignNo" type="text" auto-complete="off" placeholder="请输入事务所字号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="unifiedSocialCreditCode" label="统一社会信用代码">
                <el-input v-model="form.unifiedSocialCreditCode" type="text" auto-complete="off" placeholder="请输入统一社会信用代码" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="16">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item prop="practiceCertificateNo" label="事务所执业证书编号">
                <el-input v-model="form.practiceCertificateNo" type="text" auto-complete="off" placeholder="请输入事务所执业证书编号" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="licenseNo" label="批准执业文号">
                <el-input v-model="form.licenseNo" type="text" auto-complete="off" placeholder="请输入批准执业文号" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="approvedPracticeDate" label="批准执业日期">
                <el-date-picker
                  v-model="form.approvedPracticeDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                  placeholder="请输入批准执业日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="approvalDepartment" label="批准部门">
                <el-input v-model="form.approvalDepartment" type="text" auto-complete="off" placeholder="请输入批准部门" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item prop="organizationalForm" label="组织形式">
                <el-input v-model="form.organizationalForm" type="text" auto-complete="off" placeholder="请输入组织形式" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="methodsOfIncorporation" label="设立方式">
                <el-input v-model="form.methodsOfIncorporation" type="text" auto-complete="off" placeholder="请输入设立方式" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="numberOfPartners" label="股东合伙人数">
                <el-input v-model="form.numberOfPartners" type="text" auto-complete="off" placeholder="请输入股东合伙人数" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="registeredCapital" label="注册资本(万元)">
                <el-input v-model="form.registeredCapital" type="text" auto-complete="off" placeholder="请输入注册资本(万元)" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item prop="province" label="所属省份">
                <el-input v-model="form.province" type="text" auto-complete="off" placeholder="请输入所属省份" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="district" label="所属区县">
                <el-input v-model="form.district" type="text" auto-complete="off" placeholder="请输入所属区县" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="mailingAddress" label="通讯地址">
                <el-input v-model="form.mailingAddress" type="text" auto-complete="off" placeholder="请输入地址" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="7">
              <el-form-item prop="nameOfChargePerson" label="主任会计师/首席合伙人姓名">
                <el-input v-model="form.nameOfChargePerson" type="text" auto-complete="off" placeholder="请输入姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item prop="certificateNoOfResponsity" label="主任会计师/首席合伙人证书编号">
                <el-input v-model="form.certificateNoOfResponsity" type="text" auto-complete="off" placeholder="请输入主任会计师/首席合伙人证书编号" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item prop="postalCode" label="邮政编码">
                <el-input v-model="form.postalCode" type="text" auto-complete="off" placeholder="请输入邮政编码" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item prop="fax" label="传真">
                <el-input v-model="form.fax" type="text" auto-complete="off" placeholder="请输入传真" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="7">
              <el-form-item prop="contacts" label="联系人">
                <el-input v-model="form.contacts" type="text" auto-complete="off" placeholder="请输入联系人" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item prop="contactNumber" label="联系电话（财政系统）">
                <el-input v-model="form.contactNumber" type="text" auto-complete="off" placeholder="请输入联系电话（财政系统）" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item prop="phoneNumberOfContactPerson" label="联系人手机号">
                <el-input v-model="form.phoneNumberOfContactPerson" type="text" auto-complete="off" placeholder="请输入联系人手机号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="7">
              <el-form-item prop="operator" label="操作人">
                <el-input v-model="form.operator" type="text" auto-complete="off" placeholder="请输入操作人" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item prop="operatorNumber" label="事务所联系电话">
                <el-input v-model="form.operatorNumber" type="text" auto-complete="off" placeholder="请输入事务所联系电话" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item prop="phoneNumberOfOperator" label="操作人手机号">
                <el-input v-model="form.phoneNumberOfOperator" type="text" auto-complete="off" placeholder="请输入操作人手机号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" class="marginTop46">
            <el-col :span="24">
              <el-form-item>
                <el-button
                  :loading="loading"
                  size="medium"
                  type="primary"
                  @click.native.prevent="save"
                >
                  <span v-if="!loading">保 存</span>
                  <span v-else>保 存 中...</span>
                </el-button>
                <el-button
                  size="medium"
                  type="default"
                  @click.native.prevent="cancel"
                >
                  <span>取消</span>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { editOffice,addOffice,getOffice } from "@/api/system/office";
export default {
  name: "OfficeEdit",
  data() {
    return {
      labelPosition: "top",
      form: {
      },
      rules: {
        firmName: [
          { required: true, trigger: "blur", message: "事务所名称不能为空" },
          { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
        ],
       /* officeNumber: [{ required: true, trigger: "blur", message: "事务所编号不能为空" }], // 事务所编号*/
        practiceCertificateNo: [{ required: true, trigger: "blur", message: "事务所执业证书编号不能为空" }], // 事务所执业证书编号
        officeSignNo: [{ min: 2, max: 100, message: '长度在 2 到 50 个字符', trigger: 'blur' }], // 事务所字号
        firmStatus: [{ required: true, trigger: "blur", message: "请选择事务所状态" }], // 事务所状态
        // typeOfFirm: '', // 事务所类型
        // certificateNoOfResponsity: '', // 主任会计师/首席合伙人证书编号 50
        nameOfChargePerson: [
          { required: true, trigger: "blur", message: "注册会计师数量不能为空" },
          { max: 10, message: '长度不能超过 10 个字符', trigger: 'blur' }
        ], // 负责人姓名
        // organizationalForm: '', // 组织形式
        // methodsOfIncorporation: '', // 设立方式 20
        haveSecuritiesQualification: [{ required: true, trigger: "blur", message: "请选择是否拥有证券资格" }], // 是否拥有证券资格 2
        // haveHShareQualification: '', // 是否拥有H股资格 2
        province: [{ required: true, trigger: "blur", message: "所属省份不能为空" }], // 所属省份 20
        district: '', // 所属区县 20
        unifiedSocialCreditCode: [{ required: true, trigger: "blur", message: "统一社会信用代码不能为空" }], // 统一社会信用代码 50
        licenseNo: [{ required: true, trigger: "blur", message: "批准执业文号不能为空" }], // 批准执业文号 50
        approvedPracticeDate: [{ required: true, trigger: "blur", message: "批准执业日期不能为空" }], // 批准执业日期 20
        // approvalDepartment: '', // 批准部门 20
        // numberOfPartners: '', // 股东合伙人数 5
        numberOfCpas: [
          { required: true, trigger: "blur", message: "注册会计师数量不能为空" },
          { max: 10, message: '长度不能超过 10 个字符', trigger: 'blur' }
        ], // 注册会计师数量 5
        // numberOfBranchRegisteredEngineers: '', // 分所注册师数量 5
        // registeredCapital: '', // 注册资本（万元）10
        mailingAddress: [{ required: true, trigger: "blur", message: "通信地址不能为空" }], // 通信地址 100
        postalCode: [{ required: true, trigger: "blur", message: "邮政编码不能为空" }], // 邮政编码 10
        // fax: '', // 传真 20
        contacts: [{ required: true, trigger: "blur", message: "联系人不能为空" }], // 联系人 10
        contactNumber: [{ required: true, trigger: "blur", message: "联系电话（财政系统）不能为空" }], // 联系电话 20
        phoneNumberOfContactPerson: [
          { required: true, trigger: "blur", message: "联系手机号不能为空" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          }
        ], // 联系人手机号 20
        operator: [{ required: false, trigger: "blur", message: "操作人不能为空" }], // 联系人 10
        operatorNumber: [{ required: false, trigger: "blur", message: "事务所联系电话不能为空" }], // 联系电话 20
        phoneNumberOfOperator: [
          { required: false, trigger: "blur", message: "操作人手机号不能为空" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          }
        ], //操作人手机号 20
        placeOfBusiness: [{ required: true, trigger: "blur", message: "经营场所不能为空" }], // 经营场所 100
        headOfficeOrBranchOffice: [{ required: true, trigger: "change", message: "请选择总所/分所" }], // 总所分所 10
      },
      loading: false,
      redirect: undefined
    };
  },
  created() {
    let officeId = this.$route.query.officeId;
    if(officeId){
      getOffice(officeId).then(res=>{
        if(res.msg && res.msg == "无权限"){
          this.$message({
            message: "无权限!",
            type: 'error'
          });
          this.$router.push("/office/list")
        }else {
          this.form = res.data;
        }
      });
    }
    console.log(typeof this.form.headOfficeOrBranchOffice)
  },
  methods: {
    cancel(){
      this.$store.dispatch("tagsView/delView", this.$route);
      this.colseCurWin();
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true;
          this.form.status = 1;
          if(this.form.officeId){
            editOffice(this.form).then(res=>{
              this.loading = false;
              this.$confirm('修改成功', '提示', {
                confirmButtonText: '返回',
                showClose: false,
                showCancelButton: false,
                type: 'warning'
              }).then(() => {
                this.$store.dispatch("tagsView/delView", this.$route);
                this.colseCurWin();
              });
            }).catch(()=>{
              this.loading = false;
            });
          }else{
            addOffice(this.form).then(res=>{
              this.loading = false;
              this.$confirm('新增成功', '提示', {
                confirmButtonText: '返回',
                showClose: false,
                showCancelButton: false,
                type: 'warning'
              }).then(() => {
                this.$store.dispatch("tagsView/delView", this.$route);
                this.colseCurWin();
              });
            }).catch(()=>{
              this.loading = false;
            });
          }
        }
      });
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-form-item__label{
    position: relative;
    font-size: 15px;
  }
  ::v-deep .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before {
    content: '';  // 去掉 * 号
  }
  ::v-deep .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:after {
    content: '*';  // 后置 * 号
    color: #ff4949;
    margin-right: 4px;
    right: -15px;
    top: 2px;
    position: absolute;
  }
  ::v-deep .el-input__inner{
    &::-webkit-input-placeholder {
      font-size: 13px;
    }
  }
  ::v-deep .el-date-editor.el-input, ::v-deep .el-select{
    width: 100%;
  }
</style>
